import { defineStore } from 'pinia'

export const useUser = defineStore('user', {
  state: () => ({
    token: null
  }),
  actions: {
    loginUser(username: string, password: string): Promise<void> {
      const router = useRouter()
      return new Promise((resolve, reject) => {
        http
          .post('user/login', { username, password })
          .then(res => {
            const { token } = res.data
            const tokenCookie = useCookie('token')
            tokenCookie.value = token
            router.push('/')
            resolve()
          })
          .catch(err => {
            reject(err)
          })
      })
    }
  }
})
